package com.canxi.user.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.canxi.common.constant.CacheConstants;
import com.canxi.common.constant.Constants;
import com.canxi.common.core.controller.BaseController;
import com.canxi.common.core.domain.R;
import com.canxi.common.enums.DeviceType;
import com.canxi.common.utils.StringUtils;
import com.canxi.common.utils.redis.RedisUtils;
import com.canxi.user.domain.bo.CxUserBo;
import com.canxi.user.domain.vo.CxUserVo;
import com.canxi.user.service.IUserService;
import com.canxi.user.utils.CXLoginUtils;
import com.canxi.user.vo.CXLoginVo;
import com.canxi.user.vo.LoginVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;

/**
 * @Author: canxi
 * @Date: 2022/11/1 11:10
 * @Description:
 * @Package_name:com.canxi.core.app.vo
 */

@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/user/user")
public class LoginController extends BaseController {

    @Autowired
    private IUserService userService;


    /***
     * 帐号密码登录
     * @param phone     手机号
     * @param password  密码
     * @param loginType  默认为0 类型 web("0", "网页"),android("1", "安卓"),ios("2", "ios"),xcx("3", "小程序"),gzh("4", "公众号");
     * @return
     */
    @SaIgnore
    @PostMapping("/login")
    public R<LoginVo> login(@RequestBody CXLoginVo vo) {

       if (ObjectUtil.isNull(vo) && (StrUtil.isBlank(vo.getUsername()) || StrUtil.isBlank(vo.getPassword()))){
           R.fail("参数不全");
       }

        CxUserVo userVo =  userService.queryByPhone(vo.getUsername());
       if(ObjectUtil.isNull(userVo)){
           return R.fail("帐号/密码不正确");
       }

        String password = userService.queryPasswordById(userVo.getId());


        System.out.println(BCrypt.hashpw("123456"));
        boolean checkpw = BCrypt.checkpw(vo.getPassword(), password);
       if (ObjectUtil.isNull(userVo) || !checkpw){
           return R.fail("帐号/密码不正确");
       }

        CXLoginUtils.refreshUser(userVo, true, DeviceType.APP,vo.getLoginType());
        LoginVo loginVo = LoginVo.builder().user(userVo).token(StpUtil.getTokenValue()).build();

        return R.ok("登录成功", loginVo);

    }

    /****
     * * 手机号登录
     * @param phone  手机号
     * @param code    验证码
     * @param loginType 默认为0 类型 web("0", "网页"),android("1", "安卓"),ios("2", "ios"),xcx("3", "小程序"),gzh("4", "公众号");
     * @return
     */
    @SaIgnore
    @PostMapping("/phoneLogin")
    public R<LoginVo> phoneLogin(@RequestBody CXLoginVo vo){

        if (ObjectUtil.isNull(vo) && (StringUtils.isBlank(vo.getUsername()) || StringUtils.isBlank(vo.getCode()))){
            R.fail("参数不全");
        }
        // 保存验证码信息
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + vo.getUsername();

        String captcha = (String)RedisUtils.getCacheObject(verifyKey);
        if (StringUtils.isBlank(captcha)){
            return   R.fail("验证码错误");
        }
        if (!captcha.equals(vo.getCode())){
          return   R.fail("验证码错误");
        }
        CxUserVo userVo =  userService.queryByPhone(vo.getUsername());
        if (ObjectUtil.isNull(userVo)){
            CxUserBo userBo = new CxUserBo();
            userBo.setPhone(vo.getUsername());
            userBo.setUtype(vo.getLoginType());//类型
            userService.insertByBo(userBo);

            userVo = BeanUtil.toBean(userBo, CxUserVo.class);
            CXLoginUtils.refreshUser(userVo, true, DeviceType.APP,vo.getLoginType());

            LoginVo loginVo = LoginVo.builder().user(userVo).token(StpUtil.getTokenValue()).build();

            return R.ok("登录成功", loginVo);
        }
        CXLoginUtils.refreshUser(userVo,true,DeviceType.APP,vo.getLoginType());

        LoginVo loginVo = LoginVo.builder().user(userVo).token(StpUtil.getTokenValue()).build();

        return R.ok("登录成功", loginVo);
    }

    /*****
     * 获取验证码
     * @param phone 手机号
     * @return
     */
    @SaIgnore
    @GetMapping("/getCode")
    public R<Void> getCode(String phone) {

        // 保存验证码信息
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone;
        String code = "123456";
        RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));





        return R.ok("获取成功");
    }







}


